﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace CloudBlobMetadata
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		Microsoft.WindowsAzure.CloudStorageAccount m_stgAcct;

		public MainWindow()
		{
			try
			{
				m_stgAcct = CloudStorageAccount.Parse( AzureInterop.CloudBlobHelper.BuildConnectString(
						Properties.Settings.Default.AzureAccountName,
						Properties.Settings.Default.AzureAccountSharedKey,
						Properties.Settings.Default.AzureEndpointsProtocol)
				);

				InitializeComponent();

				// Tell MetaDataView which TreeView to watch
				this.metadataView1.TreeView = treeView1;
			}
			catch (Exception ex)
			{
				ShowExceptionDialog(ex);
			}

		}

		#region Button event handlers
		private void btnRefresh_Click(object sender, RoutedEventArgs e)
		{
			try
			{
				// protect against duplicate accts; separate Add Acct & Refresh (or refresh on node ctxt menu)
				// TODO: need better mechanism when multi-acct enabled
				treeView1.Items.Clear();
				// Create the root / AzureAcct node; AzureTreeViewItem takes care of its own event hookups
				treeView1.Items.Add(AzureInterop.Windows.Controls.AzureTreeViewItem.CreateTreeViewItem(m_stgAcct));
			}
			catch (Exception ex)
			{
				ExceptionDialog exceptionDialog = new ExceptionDialog(ex);
				exceptionDialog.ShowDialog();
			}
		}
		#endregion

		#region Exception dialog
		void host_Unloaded(object sender, RoutedEventArgs e)
		{
			this.Activate();
		}

		private void ShowExceptionDialog(Exception ex)
		{
			ExceptionDialog exceptionDialog = new ExceptionDialog(ex);
			exceptionDialog.ShowDialog();
		}
		#endregion
	}
}
